package com.liu.thread.ch09;

public class T9 extends Thread {
	
	private static int ticket=20;
	private static boolean flag=true;
	@Override
	public void run() {
		while(true){
			if(flag){
				flag=sellTicket();
			}else{
				break;
			}
		}
		
	}
	
	public synchronized boolean sellTicket(){
		
		if(ticket>0){
			try {
				Thread.currentThread().sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"售票。票号为："+ticket);
			ticket=ticket-1;
			return true;
		}else{
			System.out.println("票卖完了、、、");
			return false;
		}
		
	}
	

}
